<?php



namespace App\Http\Controllers;


use App\Models\CustomerRecords;
use App\Models\District;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{

    public function index()
    {
//        $app = app();
//        return $app->version();
    }

    public function region()
    {
        $data = District::where('level', '<>', 4)->get();
        $options = $this->parseDistrict($data, 0);
        return $this->resArray($options);
    }

    /**
     * @param Collection $data
     * @param $id
     * @return array
     * @author thanatos thanatos915@163.com
     */
    public function parseDistrict($data, $id)
    {
        $res = $data->where('upid', $id);
        $options = [];
        foreach ($res as $item) {
            $tmp = [
                'value' => $item->id,
                'label' => $item->name,
            ];
            if ($data->where('upid', $item->id)->count() > 0) {
                $tmp['children'] = $this->parseDistrict($data, $item->id);
            } else {
                $tmp['isLeaf'] = true;
            }
            array_push($options, $tmp);
        }
        return $options;
    }

}
